package com.bonc.ioc.service.impl;

import com.alibaba.fastjson.JSON;
import com.bonc.ioc.base.page.PageResult;
import com.bonc.ioc.base.service.BaseService;
import com.bonc.ioc.dao.ShareDao;
import com.bonc.ioc.model.ShareInfo;
import com.bonc.ioc.service.IShareService;
import com.bonc.ioc.vo.ShareVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * 分享相关接口
 * @author ZhangXu
 * @date 2021/3/18 16:55
 */
@Service
public class ShareServiceImpl extends BaseService implements IShareService {
    private static final Logger log = LoggerFactory.getLogger(ShareServiceImpl.class);

    @Resource
    private ShareDao shareDao;

    @Override
    public Integer addShare(ShareVo param) {
        try {
            ShareInfo isShare = shareDao.queryShareByTripleId(param);
            if (isShare == null) {
                return shareDao.addShare(param);
            }else {
                param.setId(isShare.getId());
                return shareDao.updateShareEnable(param);
            }
        }catch (Exception e ) {
            return -1;
        }
    }

    @Override
    public PageResult queryShare(ShareVo param) throws Exception {
        HashMap<String, Object> map = this.getPageParams();
        map.put("userId", param.getUserId());
        map.put("shareType", param.getShareType());
        map.put("text", param.getText());
        boolean flag1 = StringUtils.isNotBlank(param.getStartTime());
        boolean flag2 = StringUtils.isNotBlank(param.getEndTime());
        log.info("分享 获取分享列表 时间判断：" + flag1 +", " + flag2 );
        if ( flag1 && flag2 ){
            map.put("startTime", param.getStartTime());
            map.put("endTime", param.getEndTime());
        }
        log.info("分享 获取分享列表 查询参数={}", JSON.toJSONString(map));
        return new PageResult(map, shareDao.selectShareListByPage(map));
    }

    @Override
    public Integer deleteShare(ShareVo param) {
        try {
            return shareDao.updateShareTombstone(param);
        }catch (Exception e ) {
            return -1;
        }
    }
}
